﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace LogisticsCrawler.Core
{
    public class ManagerCrawlerCompany:IDisposable
    {
        private SqlConnection _conn;            	// 连接器
        private SqlCommand _cmd;                	// 命令器
        private SqlTransaction _trans;          	// 事务器
		private string _sqlcmd;                     // 命令语句
		private string _connString;                 // 连接串
		private string _message;                    // 错误消息
		private string _tableName = "CrawlerCompany";      // 表名

		#region 对外接口:错误消息
		/// <summary>
		/// 错误消息
		/// </summary>
		public string Message
		{
			get { return this._message; }
		}
		#endregion

		#region 构造函数
		/// <summary>
		/// 使用默认配置，进行数据库连接
		/// </summary>
		public ManagerCrawlerCompany()
		{
            _connString = ConfigurationManager.ConnectionStrings["LogisticsConnectionString"].ConnectionString;
		}

		/// <summary>
		/// 指定数据库连接串
		/// </summary>
        public ManagerCrawlerCompany(string connString)
		{
			_connString = connString;
		}

        ~ManagerCrawlerCompany() { }
        public void Dispose() { }

		#endregion       

        //----------------------------查询方法-----------------------------//

        #region IList<CrawlerCompany> SelectAll()
        public IList<CrawlerCompany> SelectAll()
        {
            CrawlerCompany dto = new CrawlerCompany();
            IList list = DbCommon.SelectDtoFromDb(
                this._connString, this._tableName, dto, string.Empty, ref this._message);

            if (list != null && list.Count > 0)
            {
                IList<CrawlerCompany> listres = new List<CrawlerCompany>();
                foreach (CrawlerCompany c in list)
                {
                    listres.Add(c);
                }
                return listres;
            }
            else
                return null;
        }
        #endregion

        /// <summary>
        /// 按条件查询
        /// </summary>
        /// <param name="where"></param>
        #region IList<CrawlerCompany> SelectByWhere(string where)
        public IList<CrawlerCompany> SelectByWhere(string where)
        {
            CrawlerCompany dto = new CrawlerCompany();
            IList list = DbCommon.SelectDtoFromDb(
                this._connString, this._tableName, dto, where, ref this._message);

            if (list != null && list.Count > 0)
            {
                IList<CrawlerCompany> listres = new List<CrawlerCompany>();
                foreach (CrawlerCompany c in list)
                {
                    listres.Add(c);
                }
                return listres;
            }
            else
                return null;
        }
        #endregion

        //----------------------------检测方法-----------------------------//
        /// <summary>
        /// 判断是否能够添加
        /// </summary>
        /// <param name="name">公司名称</param>
        #region bool IsCanInsert(string name)
        public bool IsCanInsert(string name)
        {
            // 统计出使用个数，1为已经占用，0为未占用
            this._sqlcmd = @"SELECT count(*) FROM CrawlerCompany where Name=@Name";
            SqlParameter param = new SqlParameter("@Name", name);
            this._conn = new SqlConnection(this._connString);
            try
            {
                int result = (int)DbCommon.ExcuteScalar(this._conn, this._sqlcmd, param);
                return result == 0 ? true : false;

            }
            catch (Exception err)
            {
                this._message = err.Message;
                return false;
            }
            finally
            {
                this._conn.Close();
            }
        }
        #endregion

        //----------------------------增删改方法-----------------------------//
        /// <summary>
        /// 新增
        /// </summary>
        /// <returns>成功与否</returns>
        #region bool Insert(CrawlerCompany dto)
        public bool Insert(CrawlerCompany dto)
        {
            if (IsCanInsert(dto.Name))
                return DbCommon.InsertDtoToDb(this._connString, this._tableName, dto, 0, false, 0, ref this._message, true);
            else
                return false;
        }
        #endregion

        /// <summary>
        /// 更新一条下载记录记录
        /// </summary>
        /// <returns></returns>
        #region bool UpdateDown(CrawlerCompany dto)
        public bool UpdateDown(CrawlerCompany dto)
        {
            return DbCommon.UpdateDtoToDb(this._connString, this._tableName, dto, 0, false, 0, ref this._message);
        }
        #endregion

        /// <summary>
        /// 删除一条下载记录记录(物理删除）
        /// </summary>
        /// <returns></returns>
        #region bool DeleteDownPhysical(CrawlerCompany dto)
        public bool DeleteDownPhysical(CrawlerCompany dto)
        {
            return DbCommon.DeleteDtoToDb(this._connString, this._tableName, dto, 0, ref this._message);
        }
        #endregion
    }
}
